package cn.jekun.shop.web.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.shop.common.service.RegService;
import cn.jekun.shop.common.service.ShopBaseAction;

/**
 * 注册会员
 * 
 * @author twj
 * 
 */
@SuppressWarnings("serial")
public class RegAction extends ShopBaseAction {
	@Resource(name = RegService.BEAN_NAME)
	RegService regService;
	public String register(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			String password = this.request.getParameter("password");//注册密码
			String validateCode = this.request.getParameter("validateCode");//手机短信验证码
			String come = this.request.getParameter("come");//商家代码
//			String email = this.request.getParameter("email");
			jsonObject = regService.register(mobile,password,validateCode,come,null,Constants.MC_MEMBER_TYPE.WEB);
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "网站用户注册"+mobile);//记录操作日志
			
			} catch (BusinessException e) {
				jsonObject = JSONObject.fromObject(e.getErrorMsg());
			} catch (Exception e) {
				jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
				e.printStackTrace();
			}
			return this.writeJson(jsonObject);
		
	}
	/**
	 * 验证手机号是否注册
	 * @return
	 */
	public String validateMobile(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			this.validateSign(mobile+"_sms_");
			jsonObject = regService.validateMobile(mobile);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	
	}
	/**
	 * 发送验证码
	 * @return
	 */
	public String sendValidateCode(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			this.validateSign(mobile+"_sms_");
			jsonObject = regService.sendValidateCodeByReg(mobile);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
}
